(function(){
	$.fn.extend({
		nice_file: function(options) {
			var defaults = {
				title: "Click to select file...",
				cssContainer: {
				},
				cssDisplay: {
					height: 16,
					overflow:"hidden",
					padding:"2px",
					border:"1px solid #7F9DB9",
					lineHeight: "16px"
				}
			};
			
			options = $.extend(true, defaults, options);
			
			return this.each(function(){
				var el = $(this);
				
				// If element is hidden dimensions will be 0
				// so we gotta clone element, insert into DOM, 
				// get its dimensions and finaly remove clone
				var c = el.clone().appendTo(document.body);
				var w = c.width();
				var h = c.height();
				c.remove();
				
				// hybrid container
				var c = $('<div class="file-input-container" title="'+(options.title)+'" />');
				c.css({
					width: w,
					height: h,
					position: "relative"
				});
				c.css(options.cssContainer);
				
				// hybrid file name display
				var d = $('<div class="file-input-display">Select file...</div>');
				d.css(options.cssDisplay);

				c.append(d);
				
				
				// hide original element and show hybrid
				el.after(c);
				c.prepend(el);
				el.nice_file_events();
				
				el.css({
					position: "absolute",
					zIndex: 2,
					left: 0,
					top: 0,
					margin:0,
					opacity: 0
				});
			});
		},
		nice_file_events: function() {
			var el = this;
			el.change(function(){ 
				$(this).parent().find(".file-input-display").html(this.value);
			});
		}
	});
})(jQuery);